// lockcontrol.txt
// A simple but handy script for levers etc. that change one terrain spot.
// If used but already changed, changes to original terrain type.
// If it finds terrain of the same type next to the target loc, changes
// them too (Useful if a gate has 2 or more portcullises).
// Memory Cells:
//   0 - Lock level. If 0, no lock.
//   1, 2 - SDF for the lock. Set to 1 when unlocked. Optionally set to 2 when controls used.
//   3 - Sp. item needed. 0 for nothing.
//   4, 5 - target loc.
//   6 - terrain to change to.
//   7 - If 1 and a SDF is given, the flag is set to 2, the controls freeze and can't be turned another time.

beginterrainscript;
variables;
    int switch = 0;
    int result;
    int choice;
    int lock;
    int frozen = 0;
    int x, y;
    int te1, te2;
body;
beginstate INIT_STATE;
    x = get_memory_cell(4);
    y = get_memory_cell(5);
    lock = get_memory_cell(0);
    te1 = get_terrain(x, y);
    te2 = get_memory_cell(6);
    if (get_memory_cell(7) != 0)
        if (get_flag(get_memory_cell(1), get_memory_cell(2)) == 2)
            frozen = 1;
    set_mechanism_difficulty(get_memory_cell(0));
    set_physical_strength(2 * get_memory_cell(0));
    if ((get_memory_cell(1) > 0) || (get_memory_cell(2) > 0))
        if (get_flag(get_memory_cell(1), get_memory_cell(2)) != 0)
            lock = 0;
break;
beginstate START_STATE;

break;
beginstate STEP_INTO_SPOT_STATE;
     set_state_continue(SEARCH_STATE);
break;
beginstate SEARCH_STATE;
    if ((lock > 0) && (get_memory_cell(3) != 0) && (has_special_item(get_memory_cell(3)))) {
           print_str_color("Your key has done its trick.", 2);
           award_party_xp(BASE_TRAP_XP, 1 + 2 * get_mechanism_difficulty());
           if ((get_memory_cell(1) > 0) || (get_memory_cell(2) > 0))
               set_flag(get_memory_cell(1), get_memory_cell(2), 1);
           lock = 0;
           }
    if (lock > 0)
    {
        reset_dialog();
        if (terrain_in_this_spot() == 400)
            add_dialog_str(0, "There are dozens of buttons on this desk. Sadly, it's got a lid. And the lid is sealed with two inches thick iron chain and a padlock.", 0);
        if (terrain_in_this_spot() == 272)
            add_dialog_str(0, "You have found a wheel. A chain is wrapped around it several times and then disappears through a hole. However, there is a huge padlock and another chain preventing the wheel from moving.", 0);
        if ((terrain_in_this_spot() == 374) || (terrain_in_this_spot() == 375))
            add_dialog_str(0, "You have found a stout, wooden lever. You can't pull it because it's sealed with chains and a lock.", 0);
        if (get_memory_cell(3) != 0)
            add_dialog_str(1, "You think you will need a key to get it open.", 0);
        add_dialog_choice(0, "Leave it.");
        add_dialog_choice(1, "Attempt to pick the lock."); 
        add_dialog_choice(2, "Attempt to break the lock."); 
        choice = run_dialog(0);
           if (choice == 1)
                block_entry(1);
           if (choice == 2)
                set_state_continue(10);
           if (choice == 3)
                set_state_continue(11);
    }
    else if (frozen == 0) {
        if (terrain_in_this_spot() == 400)
            reset_dialog_preset_options(4);
        if (terrain_in_this_spot() == 272)
            reset_dialog_preset_options(8); 
        if ((terrain_in_this_spot() == 374) || (terrain_in_this_spot() == 375))
            reset_dialog_preset_options(2);
        choice = run_dialog(0);
        if (choice == 2)
        {
            if (terrain_in_this_spot() == 400) 
                 play_sound(37);
            if ((terrain_in_this_spot() == 374) || (terrain_in_this_spot() == 375)) {
                flip_terrain(my_loc_x(), my_loc_y());
                force_instant_terrain_redraw();
                play_sound(94);
            }
            play_sound(99);
            if (switch == 0)
                set_state_continue(12);
            if (switch == 1)
                set_state_continue(13);
        }
}
break;
beginstate 11;
       if (get_highest_skill(0) > get_physical_strength()) {
           play_sound(129);
           print_big_str("You have managed to break the padlock. (Difficulty:", get_memory_cell(0), ")");
           lock = 0;
           if ((get_memory_cell(1) > 0) || (get_memory_cell(2) > 0))
               set_flag(get_memory_cell(1),get_memory_cell(2),1);
           }
       else {
           print_big_str("Failed: You're not strong enough. (Difficulty:", get_memory_cell(0), ")");
           play_sound(41);
           block_entry(1);
           }
break;
beginstate 10;
       result = run_pick_lock(get_mechanism_difficulty());
       if (result == 1) {
           award_party_xp(BASE_TRAP_XP, 1 + 2 * get_mechanism_difficulty());
           if ((get_memory_cell(1) > 0) || (get_memory_cell(2) > 0))
               set_flag(get_memory_cell(1),get_memory_cell(2),1);
           lock = 0;
           }
       else block_entry(1);
break;
beginstate 12;
       if (get_terrain(x - 1, y - 1) == te1)
       set_terrain(x - 1, y - 1, te2);
       
       if (get_terrain(x, y - 1) == te1)
       set_terrain(x, y - 1, te2);
       
       if (get_terrain(x + 1, y - 1) == te1)
       set_terrain(x + 1, y - 1, te2);
       
       if (get_terrain(x - 1, y) == te1)
       set_terrain(x - 1, y, te2);
       
       if (get_terrain(x, y) == te1)
       set_terrain(x, y, te2);
       
       if (get_terrain(x + 1, y) == te1)
       set_terrain(x + 1, y, te2);
       
       if (get_terrain(x - 1, y + 1) == te1)
       set_terrain(x - 1, y + 1, te2);
       
       if (get_terrain(x, y + 1) == te1)
       set_terrain(x, y + 1, te2);
       
       if (get_terrain(x + 1, y + 1) == te1)
       set_terrain(x + 1, y + 1, te2);
       
       force_instant_terrain_redraw();
       switch = 1;
       if (get_memory_cell(7) != 0) {
           set_flag(get_memory_cell(1), get_memory_cell(2)); 
           frozen = 1;
           }
       end();
break;
beginstate 13;
       if (get_terrain(x - 1, y - 1) == te2)
       set_terrain(x - 1, y - 1, te1);
       
       if (get_terrain(x, y - 1) == te2)
       set_terrain(x, y - 1, te1);
       
       if (get_terrain(x + 1, y - 1) == te2)
       set_terrain(x + 1, y - 1, te1);
       
       if (get_terrain(x - 1, y) == te2)
       set_terrain(x - 1, y, te1);
       
       if (get_terrain(x, y) == te2)
       set_terrain(x, y, te1);
       
       if (get_terrain(x + 1, y) == te2)
       set_terrain(x + 1, y, te1);
       
       if (get_terrain(x - 1, y + 1) == te2)
       set_terrain(x - 1, y + 1, te1);
       
       if (get_terrain(x, y + 1) == te2)
       set_terrain(x, y + 1, te1);
       
       if (get_terrain(x + 1, y + 1) == te2)
       set_terrain(x + 1, y + 1, te1);
       force_instant_terrain_redraw();
       switch = 0;
       if (get_memory_cell(7) != 0) {
           set_flag(get_memory_cell(1), get_memory_cell(2)); 
           frozen = 1;
           }
       end();
break;